﻿// ===============================================================================
// DotNet.Platform 开发框架 2016 版权所有
// ===============================================================================

using System.Collections.Generic;
using System.Linq;
using DotNet.Edu.Entity;
using DotNet.Collections;
using DotNet.Extensions;
using DotNet.Utility;

namespace DotNet.Edu.Service
{
    /// <summary>
    /// 培训类型表服务
    /// </summary>
    public class EduCategoryService
    {
        /// <summary>
        /// 是否存在指定名称的对象
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="name">名称</param>
        /// <returns>如果存在返回false</returns>
        public BoolMessage ExistsByName(string id, string name)
        {
            var repos = new EduRepository<EduCategory>();
            var has = repos.Exists(p => p.Name == name && p.Id != id);
            return has ? new BoolMessage(false, "输入培训类型名称已经存在") : BoolMessage.True;
        }

        /// <summary>
        /// 添加对象
        /// </summary>
        /// <param name="entity">实体</param>
        public BoolMessage Create(EduCategory entity)
        {
            var repos = new EduRepository<EduCategory>();
            repos.Insert(entity);
            return BoolMessage.True;
        }

        /// <summary>
        /// 更新对象
        /// </summary>
        /// <param name="entity">实体</param>
        public BoolMessage Update(EduCategory entity)
        {
            var repos = new EduRepository<EduCategory>();
            repos.Update(entity);
            return BoolMessage.True;
        }

        /// <summary>
        /// 保存对象
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="isCreate">是否新增</param>
        public BoolMessage Save(EduCategory entity, bool isCreate)
        {
            return isCreate ? Create(entity) : Update(entity);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="ids">主键数组</param>
        public BoolMessage Delete(string[] ids)
        {
            var repos = new EduRepository<EduCategory>();
            repos.Delete(ids);
            return BoolMessage.True;
        }

        /// <summary>
        /// 获取对象
        /// </summary>
        /// <param name="id">主键</param>
        public EduCategory Get(string id)
        {
            return new EduRepository<EduCategory>().Get(id);
        }

        public string GetIdByName(string name)
        {
            return new EduRepository<EduCategory>().Get(p => p.Name == name)?.Id;
        }

        /// <summary>
        /// 获取启用的简单对象集合(已排序)
        /// </summary>
        public List<Simple> GetSimpleList()
        {
            var repos = new EduRepository<EduCategory>();
            var query = repos.SQL.Select(p => p.Id, p => p.Name);
            return repos.Query(query)
                .Select(p => new Simple(p.Id, p.Name, p.Name.Spell()))
                .ToList();
        }

        /// <summary>
        /// 获取启用的对象集合
        /// </summary>
        public List<EduCategory> GetList()
        {
            var repos = new EduRepository<EduCategory>();
            return repos.Fetch();
        }

        /// <summary>
        /// 获取对象分页集合
        /// </summary>
        /// <param name="pageCondition">分页对象</param>
        /// <param name="name">名称关键字</param>
        public PageList<EduCategory> GetPageList(PaginationCondition pageCondition, string name)
        {
            pageCondition.SetDefaultOrder(nameof(EduCategory.Name));
            var repos = new EduRepository<EduCategory>();
            var query = repos.PageQuery(pageCondition);

            if (name.IsNotEmpty())
            {
                name = name.Trim();
                query.Where(p => p.Name.Contains(name));
            }
            return repos.Page(query);
        }
    }
}
